# -- coding:UTF8 –
import arcpy
import os
import glob

# inpath = arcpy.GetParameterAsText(0)
# outpath = arcpy.GetParameterAsText(1)
# keyword = arcpy.GetParameterAsText(2)

inpath = ur"R:\临时收集\待入完整库\土地利用规划数据202012"
outpath = ur"R:\临时收集\待入完整库\土地利用规划数据202012\allSHPs\JSYDGZQ"
path_name = u"2标准格式矢量数据"
keyword = "JSYDGZQ"


# inpath = ur"G:\收集数据\5.133\其它收到数据\2020地理国情监测数据国家下发\50重庆\1全集数据\test"
# outpath = ur"G:\收集数据\5.133\其它收到数据\2020地理国情监测数据国家下发\50重庆\1全集数据\test\testdb.gdb"
#
# ds_name = "LcrDataset"
# fc_name = "V_LCRA"
# output_fc_name = "V_LCRA"

def list_fold_with_tag(root_path, all_files):
    if os.path.isdir(root_path):
        files = os.listdir(root_path)
        for temp_file in files:
            current = os.path.join(root_path, temp_file)

            if os.path.isdir(current) and temp_file == path_name:
                # str(temp_file).lower().endswith(ext):
                all_files.append(current)
            else:
                list_fold_with_tag(current, all_files)
    return all_files


# def list_files(root_path, all_files=[], ext=None):
#     files = os.listdir(root_path)
#     for temp_file in files:
#         current = root_path + '/' + temp_file
#         if not os.path.isdir(current):
#             if ext is None:
#                 all_files.append(current)
#             else:
#                 if str(temp_file).lower().endswith(ext):
#                     all_files.append(current)
#         else:
#             list_files(current, all_files, ext)
#     return all_files
#
#
# def find_keyfiles(keywords):
#     all_files = []
#     list_files(root_path=inpath, all_files=all_files, ext=keywords)
#     # print "{0} files with keyword '{1}'".format(len(file_fold), keywords)
#     return all_files


def count_all_fcs(datalist):
    num_of_fcs = 0
    dl = datalist
    for d in dl:
        current_fc_num = arcpy.GetCount_management(d)[0]
        num_of_fcs = num_of_fcs + int(current_fc_num)
    return num_of_fcs


if __name__ == '__main__':

    # if not arcpy.Exists(outpath + "/%s" % output_fc_name):
    #     arcpy.AddMessage("============Start Merging==============")
    #     if len(fcs_with_path) >= 2:
    #         arcpy.Merge_management(fcs_with_path, outpath + "/%s" % output_fc_name)
    #         arcpy.AddMessage("finish")
    #     else:
    #         arcpy.AddMessage("less than 2 featureclasses")
    # else:
    #     arcpy.AddMessage("该合并文件已存在,不再重复执行操作")
    #
    # arcpy.AddMessage("============check result===============")
    # boolflg = int(arcpy.GetCount_management(outpath + "/%s" % fc_name)[0]) == count_all_fcs()
    # arcpy.AddMessage("合并后条目总数是否与元数据总数相符？\n{0}".format(boolflg))
    print "Start to extract data with '{0}'".format(keyword)
    fold_list = []
    folds = list_fold_with_tag(root_path=inpath, all_files=fold_list)
    no = 0
    fcCounts = 0
    for fold in folds:
        arcpy.env.workspace = fold
        files = arcpy.ListFeatureClasses(wild_card='*{}*'.format(keyword))
        for f in files:
            arcpy.CopyFeatures_management(in_features=f,
                                          out_feature_class=os.path.join(outpath, f))
            no = no + 1
            fcCounts = fcCounts + int(arcpy.GetCount_management(f)[0])
            print "NO.{0}: {1}".format(no, f)
    print "\n{0}个要素类条目总数为:{1}".format(len(folds), fcCounts)
